package com.hqs.drawingboard;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hqs.common.utils.ScreenUtils;
import com.hqs.common.utils.SharedPreferenceUtil;
import com.hqs.common.utils.ViewUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private DrawingBoardView dbView;
    private ColorPickerView colorPickerView;
    private RelativeLayout rlSize;
    private TextView tvSize;
    private SeekBar seekBar;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 设置竖屏
        ScreenUtils.setScreenOrientationPortrait(this);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        dbView = (DrawingBoardView) findViewById(R.id.drawing_board_view);

        dbView.setOnMultiClickListener(new TouchUtil.OnMultiClickListener() {
            @Override
            public void onClick(View view) {

            }

            @Override
            public void onDoubleClick(View view) {
                dbView.clear();
            }

            @Override
            public void onTrebleClick(View view) {
                dbView.clear();
            }

            @Override
            public void on2Click(View view) {
                showMenu();
            }

            @Override
            public void on2DoubleClick(View view) {
                showMenu();
            }

            @Override
            public void on2TrebleClick(View view) {
            }

            @Override
            public void on3Click(View view) {

            }

            @Override
            public void on3DoubleClick(View view) {
            }

            @Override
            public void on3TrebleClick(View view) {
            }

            @Override
            public void on4Click(View view) {
                savePic();
            }

            @Override
            public void on4DoubleClick(View view) {
            }

            @Override
            public void on4TrebleClick(View view) {
            }
        });

        colorPickerView = (ColorPickerView) findViewById(R.id.color_picker_view);
        colorPickerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fadeColorPicker();
            }
        });
        colorPickerView.setOnPickColorClickListener(new ColorPickerView.OnPickColorClickListener() {
            @Override
            public void onPickColor(int color) {
                dbView.setPaintColor(color);
                SharedPreferenceUtil.set("color", color);
            }
        });

        rlSize = (RelativeLayout) findViewById(R.id.rl_size);
        tvSize = (TextView) findViewById(R.id.tv_size);
        seekBar = (SeekBar) findViewById(R.id.seek_bar);
        rlSize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fadeSeekbar();
            }
        });

        SharedPreferenceUtil.initSharedPreference(this);

        final int color = SharedPreferenceUtil.get("color", dbView.getPaintColor());
        int width = SharedPreferenceUtil.get("size", 6);
        seekBar.setMax(40);
        seekBar.setProgress(width - 1);
        dbView.setPaintColor(color);
        setPaintWidth(width - 1);

        ViewUtil.setRoundCornerToView(tvSize, width * 0.5f * ScreenUtils.density(this), false, color);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                setPaintWidth(seekBar.getProgress());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        progressBar = (ProgressBar) findViewById(R.id.progressBar);

    }

    private void setPaintWidth(int width){
        width += 1;
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tvSize.getLayoutParams();
        layoutParams.height = (int) (width * ScreenUtils.density(MainActivity.this));
        tvSize.setLayoutParams(layoutParams);
        ViewUtil.setRoundCornerToView(tvSize, tvSize.getWidth() * 0.5f, false, dbView.getPaintColor());
        dbView.setPaintWidth(width);

        SharedPreferenceUtil.set("size", width);
    }
    private void showMenu(){

        ArrayList<String> items = new ArrayList<>();
        items.add("画笔颜色");
        items.add("笔触大小");
        items.add("保存图片");
        items.add("取消");

        new SingleSelectDialog(this, items, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                if (pos == 0){
                    colorPickerView.setVisibility(View.VISIBLE);
                    dbView.setEnabled(false);
                }
                else if (pos == 1){
                    rlSize.setVisibility(View.VISIBLE);
                    dbView.setEnabled(false);
                }
                else if (pos == 2){
                    savePic();
                }
            }
        }).show();
    }

    private void fadeColorPicker(){
        dbView.setEnabled(true);
        Animation animation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        animation.setDuration(200);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                colorPickerView.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        colorPickerView.clearAnimation();
        colorPickerView.setAnimation(animation);
        animation.start();
    }

    private void fadeSeekbar(){
        dbView.setEnabled(true);
        Animation animation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        animation.setDuration(200);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                rlSize.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        rlSize.clearAnimation();
        rlSize.setAnimation(animation);
        animation.start();
    }

    private void savePic(){
        progressBar.setVisibility(View.VISIBLE);
        dbView.setEnabled(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = dbView.getPaintedBitmap();

                final String filePath = Environment.getExternalStorageDirectory() + "/" + getPackageName() + "/";
                final String fileName = UUID.randomUUID().toString() + ".png";
                File f = new File(filePath);

                if (!f.exists()) {
                    f.mkdir();
                }
                f = new File(filePath, fileName);
                if (!f.exists()){
                    try {
                        f.createNewFile();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                try {
                    FileOutputStream out = new FileOutputStream(f);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();

                    progressBar.post(new Runnable() {
                        @Override
                        public void run() {
                            NotificationUtil.show(MainActivity.this, "文件保存完毕, 点击查看", filePath + fileName);
                            Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
                            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out);
                            animation.setDuration(200);
                            animation.setAnimationListener(new Animation.AnimationListener() {
                                @Override
                                public void onAnimationStart(Animation animation) {

                                }

                                @Override
                                public void onAnimationEnd(Animation animation) {
                                    progressBar.setVisibility(View.INVISIBLE);
                                    dbView.setEnabled(true);
                                }

                                @Override
                                public void onAnimationRepeat(Animation animation) {

                                }
                            });
                            progressBar.clearAnimation();
                            progressBar.setAnimation(animation);
                            animation.start();
                        }
                    });
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

    }

    @Override
    public void onBackPressed() {
        if(dbView.undo() == false){
            super.onBackPressed();
        }
    }

}
